﻿namespace Tools
{
    /// <summary>
    /// 复合版的Signal
    /// 集合中只要有数据发生改变，那么复合Signal就Dispath一次。
    /// </summary>
    public class MergeSignal<T>
        where T : struct
    {
        private readonly Signal s = new Signal();

        private readonly ReactiveProperty<T>[] _rps;

        public MergeSignal(ReactiveProperty<T>[] rps)
        {
            _rps = rps;
            for (var i = 0; i < _rps.Length; i++)
            {
                _rps[i].AddListener(OnDispatch);
            }
        }

        public void RemoveListener()
        {
            foreach (var rp in _rps)
            {
                rp.RemoveListener(OnDispatch);
            }
        }

        private void OnDispatch(T t)
        {
            s.Dispatch();
        }
    }
}